<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/7/23
 * Time: 13:37
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Notice extends Base
{
    /**
     * 消息首页展示
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if (request()->isAjax()) {
            extract(input());
            $where = [
                'n_type' => 1
            ];
            // 推送范围
            if (isset($n_range) && $n_range != '') {
                $where['n_range'] = $n_range;
            }
            // 标题
            if (isset($n_title) && $n_title != '') {
                $where['n_title|n_content'] = ['like', '%' . $n_title . '%'];
            }
            //开始时间查询
            if (isset($start) && $start != "" && isset($end) && $end == "") {
                $where['n_send_datetime'] = ['>=', $start];
            }
            if (isset($end) && $end != "" && isset($start) && $start == "") {
                $where['n_send_datetime'] = ['<=', $end];
            }
            if (isset($start) && $start != "" && isset($end) && $end != "") {
                $where['n_send_datetime'] = ['between', [$start, $end]];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->where($where)->count();// 获取总条数
            $lists = Db::name('notice')
                ->where($where)
                ->page($Nowpage, $limits)
                ->order('n_send_datetime desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 发布消息
     * @return mixed|\think\response\Json
     */
    public function noticeAdd()
    {
        if (Request::instance()->isPost()) {
            $params = input('param.');
            if ($params['n_range'] == 4) {
                $users = trim($params['users'], ' ');
                $users = trim($users, ',');
                if ($users == '') {
                    return json(['code' => 100, 'msg' => "推送用户不能为空"]);
                }
            }
            $time = date('Y-m-d H:i:s');
            $data = [
                'n_title' => trim($params['n_title'], ' '), // 标题
                'n_content' => trim($params['n_content'], ' '),// 内容
                'n_app_id' => 0,// 内容
                'n_type' => 1,// 内容
                'n_range' => $params['n_range'],//推送范围  1 全部  2 个别
                'n_read_user_id' => ',0,', // 已读用户
                'n_del_user_id' => ',0,', // 删除的用户
                'n_send_datetime' => $time, // 推送时间
            ];
            if ($params['n_range'] == 4) {
                $ids = Db::table('up_u_user')
                    ->where('user_phone', 'in', $users)
                    ->column('user_id');
                $data['n_user_id'] = ',' . implode($ids, ',') . ',';
            } else {
                $where = [
                    'user_status' => 1
                ];
                if ($params['n_range'] == 2) {
                    $where['user_type'] = 1;
                }
                if ($params['n_range'] == 3) {
                    $where['user_type'] = 2;
                }
                $data['n_user_id'] = 0;
                $ids = Db::table('up_u_user')
                    ->where($where)
                    ->column('user_id');
            }
            if ($ids) {
                $res = Db::table('up_notice')->insertGetId($data);
                if ($res) {
                    try {
                        $this->Jpush(1, $ids, $params['n_title'], ['id' => $res, 'n_type' => 5]);
                    } catch (\Exception $e) {
                    }
                    return json(['code' => 200, 'msg' => '发布成功']);
                } else {
                    return json(['code' => 100, 'msg' => '发布失败']);
                }
            } else {
                return json(['code' => 100, 'msg' => '此类型无推送用户！']);
            }
        }
        return $this->fetch('notice/noticeadd');
    }

    /**
     * 消息详情
     * @return mixed
     */
    public function noticeDetail()
    {
        $n_id = input('n_id');
        $info = Db::name('notice')
            ->where('n_id', $n_id)
            ->field('n_title, n_content, n_range, n_send_datetime,n_user_id')
            ->find();
        // 推送范围
        if ($info['n_range'] == 4) {
            $user = Db::table('up_u_user')
                ->field('user_id, user_username,user_phone')
                ->where('user_id', 'in', trim($info['n_user_id'], ','))
                ->select();
            $this->assign('user', $user);

        }
        $range = [
            1 => '全部',
            2 => '医生',
            3 => '业务员',
            4 => '个别'
        ];
        $this->assign('info', $info);
        $this->assign('range', $range);
        return $this->fetch('notice/noticedetail');
    }

    /**
     * 发布的用户列表
     * @return mixed|\think\response\Json
     */
    public function noticeUser()
    {
        if (request()->isAjax()) {
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $user_ids = Db::table('up_notice')->where('n_id', session('n_id'))->value('n_user_id');
            $where['user_id'] = ['IN', trim($user_ids, ',')];
            $count = Db::name('u_user')->where($where)->count();// 获取总条数
            $lists = Db::name('u_user')
                ->field('user_username, user_image, user_id,user_phone')
                ->where($where)
                ->page($Nowpage, $limits)
                ->order('user_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $n_id = input('n_id');
        session('n_id', $n_id);
        return $this->fetch('notice/noticeuser');
    }

    /**
     * 删除消息
     * @return \think\response\Json
     */
    public function noticeDel()
    {
        $n_id = input('id');
        $res = Db::table('up_notice')->delete($n_id);
        if ($res) {
            return json(['code' => 200, 'msg' => '删除成功']);
        } else {
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

}